#include "Card.h"

QHash<CardID,CardInfo*>* Card::infoHash = NULL;


Card::Card() {}

CardInfo* Card::getInfo(CardID id) {
	
	// Ensures a singleton hash AND that the hash is filled before reading it.
	if (infoHash == NULL) createInfoHash();
	
	return infoHash->value(id);
}


CardID Card::cardForProperties(CardFunction function, CardNature nature) {
	if (infoHash ==  NULL) createInfoHash();
	
	foreach (CardID id, infoHash->keys()) {
		CardInfo* nfo = infoHash->value(id);
		
		if ((nfo != NULL)
			&& (nfo->getFunction() == function)
			&& (nfo->getNature() & nature))
			return id;
	}
	
	return CardID_NoCard;
}




void Card::createInfoHash() {
	infoHash = new QHash<CardID,CardInfo*>();
	
	(*infoHash)[CardID_NoCard] = 
			new CardInfo(tr("[NoCard]"), Properties::FILENAME_CardBack, CardInfo::Function_NoFunction, CardInfo::Nature_NoNature, 0);
	
	(*infoHash)[CardID_Distance_25] = 
			new CardInfo(tr("25 km"), Properties::FILENAME_Distance_25, CardInfo::Function_Distance, CardInfo::Nature_NoNature, 25);
	(*infoHash)[CardID_Distance_50] = 
			new CardInfo(tr("50 km"), Properties::FILENAME_Distance_50, CardInfo::Function_Distance, CardInfo::Nature_NoNature, 50);
	(*infoHash)[CardID_Distance_75] = 
			new CardInfo(tr("75 km"), Properties::FILENAME_Distance_75, CardInfo::Function_Distance, CardInfo::Nature_NoNature, 75);
	(*infoHash)[CardID_Distance_100] = 
			new CardInfo(tr("100 km"), Properties::FILENAME_Distance_100, CardInfo::Function_Distance, CardInfo::Nature_NoNature, 100);
	(*infoHash)[CardID_Distance_200] = 
			new CardInfo(tr("200 km"), Properties::FILENAME_Distance_200, CardInfo::Function_Distance, CardInfo::Nature_NoNature, 200);
	
	(*infoHash)[CardID_Hazard_Accident] = 
			new CardInfo(tr("Accident"), Properties::FILENAME_Hazard_Accident, CardInfo::Function_Hazard, CardInfo::Nature_Accident, 0);
	(*infoHash)[CardID_Hazard_OutOfGas] = 
			new CardInfo(tr("Out of Gas"), Properties::FILENAME_Hazard_OutOfGas, CardInfo::Function_Hazard, CardInfo::Nature_Gas, 0);
	(*infoHash)[CardID_Hazard_FlatTire] = 
			new CardInfo(tr("Flat Tire"), Properties::FILENAME_Hazard_FlatTire, CardInfo::Function_Hazard, CardInfo::Nature_Tire, 0);
	(*infoHash)[CardID_Hazard_SpeedLimit] = 
			new CardInfo(tr("Speed Limit"), Properties::FILENAME_Hazard_SpeedLimit, CardInfo::Function_Hazard, CardInfo::Nature_Speed, 0);
	(*infoHash)[CardID_Hazard_Stop] = 
			new CardInfo(tr("Stop"), Properties::FILENAME_Hazard_Stop, CardInfo::Function_Hazard, CardInfo::Nature_Light, 0);
	
	(*infoHash)[CardID_Remedy_Repair] = 
			new CardInfo(tr("Repairs"), Properties::FILENAME_Remedy_Repair, CardInfo::Function_Remedy, CardInfo::Nature_Accident, 0);
	(*infoHash)[CardID_Remedy_Gasoline] = 
			new CardInfo(tr("Gasoline"), Properties::FILENAME_Remedy_Gasoline, CardInfo::Function_Remedy, CardInfo::Nature_Gas, 0);
	(*infoHash)[CardID_Remedy_SpareTire] = 
			new CardInfo(tr("Spare Tire"), Properties::FILENAME_Remedy_SpareTire, CardInfo::Function_Remedy, CardInfo::Nature_Tire, 0);
	(*infoHash)[CardID_Remedy_EndOfLimit] = 
			new CardInfo(tr("End of Limit"), Properties::FILENAME_Remedy_EndOfLimit, CardInfo::Function_Remedy, CardInfo::Nature_Speed, 0);
	(*infoHash)[CardID_Remedy_Roll] = 
			new CardInfo(tr("Roll"), Properties::FILENAME_Remedy_Roll, CardInfo::Function_Remedy, CardInfo::Nature_Light, 0);
	
	(*infoHash)[CardID_Safety_DrivingAce] = 
			new CardInfo(tr("Driving Ace"), Properties::FILENAME_Safety_DrivingAce, CardInfo::Function_Safety, CardInfo::Nature_Accident, 0);
	(*infoHash)[CardID_Safety_ExtraTank] = 
			new CardInfo(tr("Extra Tank"), Properties::FILENAME_Safety_ExtraTank, CardInfo::Function_Safety, CardInfo::Nature_Gas, 0);
	(*infoHash)[CardID_Safety_PunctureProof] = 
			new CardInfo(tr("Puncture-proof"), Properties::FILENAME_Safety_PunctureProof, CardInfo::Function_Safety, CardInfo::Nature_Tire, 0);
	(*infoHash)[CardID_Safety_RightOfWay] = 
			new CardInfo(tr("Right of Way"), Properties::FILENAME_Safety_RightOfWay, CardInfo::Function_Safety, CardInfo::Nature_Speed | CardInfo::Nature_Light, 0);
}
